import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Notepad extends JFrame {
    private JTextArea textArea;  // 文本区域
    private String fileName;  // 文本文件名
    // 构造方法创建记事本界面
    public Notepad() {
        createAndShowGUI();
    }
    private void createAndShowGUI() {  //创建一个容器窗口
        setTitle("记事本");
        setSize(900, 800);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        textArea = new JTextArea();
        textArea.setEditable(true);
        JScrollPane scrollPane = new JScrollPane(textArea);
        add(scrollPane);
        // 创建3个“文件”“编辑”“帮助”的JMenu菜单组件
        JMenuBar menuBar = new JMenuBar();
        JMenu fileMenu = new JMenu("文件");
        JMenu editMenu = new JMenu("编辑");
        JMenu helpMenu = new JMenu("帮助");
       // 调整菜单栏的字体大小
        Font font1=new Font("黑体", Font.BOLD,25);
        fileMenu.setFont(font1);
        editMenu.setFont(font1);
        helpMenu.setFont(font1);
        textArea.setFont(font1);
        // 创建“文件”的菜单项
        JMenuItem newFileItem = new JMenuItem("新建");
        newFileItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                newFile();
            }
        });
        JMenuItem openFileItem = new JMenuItem("打开");
        openFileItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                openFile();
            }
        });
        JMenuItem saveFileItem = new JMenuItem("保存");
        saveFileItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveFile();
            }
        });
        JMenuItem saveAsFileItem = new JMenuItem("另存为");
        saveAsFileItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saveAsFile();
            }
        });
        JMenuItem exitItem = new JMenuItem("退出");
        exitItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        fileMenu.add(newFileItem);
        fileMenu.add(openFileItem);
        fileMenu.add(saveFileItem);
        fileMenu.add(saveAsFileItem);
        fileMenu.add(exitItem);
        // 改字体大小
        newFileItem.setFont(font1);
        openFileItem.setFont(font1);
        saveFileItem.setFont(font1);
        saveAsFileItem.setFont(font1);
        exitItem.setFont(font1);

        // 创建“编辑”的菜单项
        JMenuItem copyItem = new JMenuItem("复制");
        copyItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.copy();
            }
        });
        JMenuItem cutItem = new JMenuItem("剪切");
        cutItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.cut();
            }
        });
        JMenuItem pasteItem = new JMenuItem("粘贴");
        pasteItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.paste();
            }
        });
        JMenuItem selectAllItem = new JMenuItem("全选");
        selectAllItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textArea.selectAll();
            }
        });
        editMenu.add(copyItem);
        editMenu.add(cutItem);
        editMenu.add(pasteItem);
        editMenu.add(selectAllItem);
        // 改字体大小
        copyItem.setFont(font1);
        cutItem.setFont(font1);
        pasteItem.setFont(font1);
        selectAllItem.setFont(font1);

        // 创建“帮助”的菜单项
        JMenuItem messageItem = new JMenuItem("作者信息");
        messageItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(Notepad.this, "姓名: 李观慧\n学号: 2022036143506\n学院: 计算机科学学院\n班级: 22计师2班");
            }
        });
        // 将创建的3个“文件”“编辑”“帮助”的JMenu菜单组件加入到JMenuBar中
        helpMenu.add(messageItem);
        messageItem.setFont(font1);
        menuBar.add(fileMenu);
        menuBar.add(editMenu);
        menuBar.add(helpMenu);
        setJMenuBar(menuBar);
    }
    // "文件"里面的功能方法
    // 新建文件
    private void newFile() {
        textArea.setText("");
        fileName = null;
    }
    // 打开文件
    private void openFile() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("打开文件");
        int returnValue = fileChooser.showOpenDialog(this);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                StringBuilder content = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    content.append(line).append("\n");
                }
                textArea.setText(content.toString());
                fileName = file.getName();
            } catch (IOException e) {
                JOptionPane.showMessageDialog(this, " 文件打开失败: " + e.getMessage());
            }
        }
    }
    // 保存文件
    private void saveFile() {
        if (fileName == null) {
            saveAsFile();
        } else {
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(fileName))) {
                writer.write(textArea.getText());
            } catch (IOException e) {
                JOptionPane.showMessageDialog(this, "文件保存失败: " + e.getMessage());
            }
        }
    }
    // 文件另存为
    private void saveAsFile() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setDialogTitle("文件另存为");
        int returnValue = fileChooser.showSaveDialog(Notepad.this);
        if (returnValue == JFileChooser.APPROVE_OPTION) {
            File file = fileChooser.getSelectedFile();
            try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
                writer.write(textArea.getText());
                fileName = file.getName();
                //fileNameField.setText(fileName);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(Notepad.this, "文件保存失败: " + ex.getMessage());
            }
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                Notepad notepad = new Notepad();  // 创建笔记本对象
                notepad.setVisible(true);

            }
        });
    }
}
